﻿using UnityEngine;

namespace XLua
{
    [LuaCallCSharp]
    public class LuaBehavior : MonoBehaviour
    {
        [LuaCallCSharp]
        [System.Serializable]
        public class AssetRef
        {
            public string name;
            public UnityEngine.Object uobject;
            public string value;
        }

        public AssetRef[] assetsRef;

        public string luaPath = null;

        internal string _name;

        internal LuaTable _table;
        public LuaTable luaTable
        {
            get
            {
                return _table;
            }
        }

        public static LuaTable Get(GameObject go, LuaTable table)
        {
            string mat1 = table.Get<string>("__cname");
            LuaBehavior[] cmps = go.GetComponents(typeof(LuaBehavior)) as LuaBehavior[];
            foreach (LuaBehavior cmp in cmps)
            {
                string mat2 = cmp._table.Get<string>("__cname");
                if (mat1 == mat2)
                    return cmp._table;
            }
            return null;
        }

        void Awake()
        {
            LuaManager _instance = LuaManager.Instance;
            if (!string.IsNullOrEmpty(luaPath))
            {
                _name = System.IO.Path.GetFileName(luaPath);
                var tb = LuaManager.Instance.DoTable(luaPath);
                if (tb != null)
                {
                    LuaFunction func = tb.Get<LuaFunction>("New");
                    if (func != null)
                    {
                        var tt = func.Call();
                        _table = tt[0] as LuaTable;
                    }
                    else
                    {
                        _table = null;
                    }

                    if (_table != null)
                    {
                        _table.Set("gameObject", gameObject);
                        _table.Set("transform", transform);
                        _table.Set("luaBehavior",this);
                        if (assetsRef != null)
                        {
                            foreach (var ass in assetsRef)
                            {
                                _table.Set(ass.name, ass);
                            }
                        }
                        func = _table.Get<LuaFunction>("Awake");
                        if (func != null)
                        {
                            func.Call(_table);
                            func.Dispose();
                            func = null;
                        }
                    }
                }
            }
        }

        // Start is called before the first frame update
        void Start()
        {
            if (!LuaManager.IsInited || _table == null) return;
            {
                LuaFunction func = _table.Get<LuaFunction>("Start");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnEnable()
        {
            if (!LuaManager.IsInited || _table == null) return;

            LuaFunction func = _table.Get<LuaFunction>("OnEnable");
            if (func != null)
            {
                func.Call(_table);
                func.Dispose();
                func = null;
            }
        }

        void OnDisable()
        {
            if (!LuaManager.IsInited || _table == null) return;

            LuaFunction func = _table.Get<LuaFunction>("OnDisable");
            if (func != null)
            {
                func.Call(_table);
                func.Dispose();
                func = null;
            }
        }

        public void CallLuaFun(string luaFunc)
        {
            if (!LuaManager.IsInited || _table == null) return;

            LuaFunction func = _table.Get<LuaFunction>(luaFunc);
            if (func != null)
            {
                func.Call(_table);
                func.Dispose();
                func = null;
            }
        }

        void OnDestroy()
        {
            if (!LuaManager.IsInited) return;

            if (_table != null)
            {
                LuaFunction func = _table.Get<LuaFunction>("OnDestroy");
                if (func != null)
                {
                    func.Call(_table);
                    func.Dispose();
                    func = null;
                }
                _table.Dispose();
            }
        }

    }
}
